package com.onpositive.funkytable.swt;

import java.util.ArrayList;

import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;

import com.onpositive.funkytable.IFunkyComposite;
import com.onpositive.funkytable.IFunkyControl;
import com.onpositive.funkytable.IFunkyStyle;
import com.onpositive.funkytable.ILayoutParams;
import com.onpositive.funkytable.Point;

public class FunkyComposite implements IFunkyComposite, ISWTControl {

	private FunkyStyle style;
	protected ArrayList<IFunkyControl> ct = new ArrayList<IFunkyControl>();
	protected ArrayList<ILayoutParams> lp = new ArrayList<ILayoutParams>();
	protected ArrayList<Rectangle> rp = new ArrayList<Rectangle>();
	protected int orientation;

	public int getOrientation() {
		return orientation;
	}

	public void setOrientation(int orientation) {
		this.orientation = orientation;
	}

	@Override
	public void add(IFunkyControl ctrl, ILayoutParams params) {
		this.ct.add(ctrl);
		this.lp.add(params);
		this.rp.add(new Rectangle(0, 0, 0, 0));
	}

	@Override
	public Point measure(int desiredwidth, int desiredheight) {
		if (orientation == IFunkyComposite.HORIZONTAL) {
			int a = 0;
			int cY = 0;
			int Mx=0;
			for (IFunkyControl m : ct) {
				
				ILayoutParams iLayoutParams = lp.get(a);
				Rectangle r = rp.get(a);
				r.x = Mx;
				r.y = 0;
				int dWidth = iLayoutParams.width();
				boolean fillParentWidth = dWidth == ILayoutParams.FILL_PARENT;
				if (fillParentWidth) {
					dWidth = desiredwidth;
					r.width = desiredwidth;
				} else {
										
				}
				int dHeight = iLayoutParams.height();
				boolean fillParentHeight = dHeight == ILayoutParams.FILL_PARENT;
				if (fillParentHeight) {
					dHeight = Integer.MAX_VALUE;
				}
				Point measure = m.measure(dWidth, dHeight);
				if (fillParentWidth) {
					r.width = desiredwidth;
				} else {
					if (measure.x==-1){
						System.out.println("A");
					}
					r.width = measure.x;
				}
				if (fillParentHeight) {
					r.height = measure.y;
				} else {
					r.height = measure.y;
				}
				cY=Math.max(cY,r.height);
				Mx+=r.width;
				a++;
			}
			return new Point(Mx,cY);
		} else {
			int a = 0;
			int cY = 0;
			int Mx=0;
			for (IFunkyControl m : ct) {
				
				ILayoutParams iLayoutParams = lp.get(a);
				Rectangle r = rp.get(a);
				r.y = cY;
				r.x = 0;
				int dWidth = iLayoutParams.width();
				boolean fillParentWidth = dWidth == ILayoutParams.FILL_PARENT;
				if (fillParentWidth) {
					dWidth = desiredwidth;
					if (desiredwidth==-1){
						System.out.println("A");
					}
					r.width = desiredwidth;
				} else {
					dWidth = -1;
				}
				int dHeight = iLayoutParams.height();
				boolean fillParentHeight = dHeight == ILayoutParams.FILL_PARENT;
				if (fillParentHeight) {
					dHeight = Integer.MAX_VALUE;
				}
				Point measure = m.measure(dWidth, dHeight);
				if (fillParentWidth) {
					r.width = desiredwidth;
					if (desiredwidth==-1){
						System.out.println("A");
					}
				} else {
					r.width = measure.x;
				}
				if (fillParentHeight) {
					r.height = measure.y;
				} else {
					r.height = measure.y;
				}
				cY += r.height;
				Mx=Math.max(Mx,r.width);
				a++;
			}
			return new Point(Mx,cY);
		}	
	}

	@Override
	public void setStyle(IFunkyStyle style) {
		this.style = (FunkyStyle) style;
	}

	@Override
	public IFunkyStyle getStyle() {
		return style;
	}

	@Override
	public void draw(GC gc, int x, int y, int width, int height) {
		gc.setClipping(x, y, width, height);
		if (style != null) {
			gc.setBackground(style.bgcolor);
			gc.setForeground(style.fgcolor);
		}
		gc.fillRectangle(x, y, width, height);
		int a = 0;
		for (IFunkyControl c : ct) {
			Rectangle g = getBounds(c, a);
			ISWTControl s = (ISWTControl) c;
			s.draw(gc, x + g.x, y + g.y, g.width, g.height);
			a++;
		}
	}

	private Rectangle getBounds(IFunkyControl c, int a) {
		return rp.get(a);
	}

	@Override
	public void layout(int width) {
		if (orientation == IFunkyComposite.HORIZONTAL) {
			int a = 0;
			int cY = 0;
			for (IFunkyControl m : ct) {
				ISWTControl z = (ISWTControl) m;
				z.layout(width);
				ILayoutParams iLayoutParams = lp.get(a);
				Rectangle r = rp.get(a);
				r.y = 0;
				r.x = cY;
				int dWidth = iLayoutParams.width();
				boolean fillParentWidth = dWidth == ILayoutParams.FILL_PARENT;
				if (fillParentWidth) {
					dWidth = width;
					r.width = width;
				}
				int dHeight = iLayoutParams.height();
				boolean fillParentHeight = dHeight == ILayoutParams.FILL_PARENT;
				if (fillParentHeight) {
					dHeight = Integer.MAX_VALUE;
				}
				Point measure = m.measure(dWidth, dHeight);
				if (fillParentWidth) {
					r.width = width;
				} else {
					r.width = measure.x;
				}
				if (fillParentHeight) {
					r.height = measure.y;
				} else {
					r.height = measure.y;
				}
				cY += r.width;
				a++;
			}
		} else {
			int a = 0;
			int cY = 0;
			for (IFunkyControl m : ct) {
				if (ct.size()==6){
					System.out.println('A');
				}
				ISWTControl z = (ISWTControl) m;
				z.layout(width);
				ILayoutParams iLayoutParams = lp.get(a);
				Rectangle r = rp.get(a);
				r.y = cY;
				r.x = 0;
				int dWidth = iLayoutParams.width();
				boolean fillParentWidth = dWidth == ILayoutParams.FILL_PARENT;
				if (fillParentWidth) {
					dWidth = width;
					r.width = width;
				} else {
					dWidth = -1;
				}
				int dHeight = iLayoutParams.height();
				boolean fillParentHeight = dHeight == ILayoutParams.FILL_PARENT;
				if (fillParentHeight) {
					dHeight = Integer.MAX_VALUE;
				}
				Point measure = m.measure(dWidth, dHeight);
				if (fillParentWidth) {
					r.width = width;
				} else {
					r.width = measure.x;
				}
				if (fillParentHeight) {
					r.height = measure.y;
				} else {
					r.height = measure.y;
				}
				cY += r.height;
				a++;
			}
		}
	}

}
